package com.ashley.apptools.vo;

import java.util.ArrayList;

import com.ashley.apptools.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

	private ArrayList<ItemVo> arrayList;
	private Context mContext;
	public MyAdapter(Context context,ArrayList<ItemVo> list) {
		
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.arrayList = list;
	}
	public ArrayList<ItemVo> data()
	{
		return arrayList;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arrayList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return arrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Viewhand viewhand = null;
		final int id =position;
		if(convertView == null)
		{
			LayoutInflater layoutInflater=LayoutInflater.from(mContext);
			convertView=layoutInflater.inflate(R.layout.item, null);
			viewhand = new Viewhand();
			viewhand.itemNo = (TextView) convertView.findViewById(R.id.item_no);
			viewhand.freight = (TextView) convertView.findViewById(R.id.freihgt_item);
			ImageButton button = (ImageButton) convertView.findViewById(R.id.imageButton1);
			button.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(arrayList.get(id) != null)
					{
						arrayList.remove(id);
						notifyDataSetChanged();
					}

				}
			});
			convertView.setTag(viewhand);
		}else
		{
			viewhand = (Viewhand) convertView.getTag();
		}
		viewhand.itemNo.setText(arrayList.get(position).getItemNo());
		viewhand.freight.setText(arrayList.get(position).getFreight()+"");
		return convertView;
	}
	private static class Viewhand
	{
		TextView itemNo;
		TextView freight;
	}

}
